//获取应用实例
let app = getApp();
let httptool=require("../../utils/zry.httptool.js");
Page({
  data:{
    forumArea:true,//帖字是否显示
    hotArea:false,//热搜词是否显示
    propName:true,//擂盟提示是否显示
    searchArea:false,//头部搜索框是否显示
    hotList:[],//热门搜索圈子
    CircleName:'',//圈子名称
    results_data:[],//输入搜索名称后的提示擂盟
    page: { PageIndex: 1, PageSize: 10 },
    resultForums:{},//搜索后的擂盟，帖子结果
    topics:[],//搜索的帖子
    CircleDetail:{},//搜索后的擂盟详情
    circleId:'',//输入名称时出的擂盟id
    circleParam:{},//进入擂盟详情时所需参数
    msgprop:'',//消息提示 
    noData: false,
    noDataMsg:"此擂盟还没有帖子哦！"
  },
  search:function(e){
      let that=this;
      that.setData({CircleName:e.detail.value});
      that.searchResult();
  },
  cancelSearch:function(){
    let that=this;
    that.setData({propName:true});
  },
  onLoad:function(options){
    // 页面初始化 options为页面跳转所带来的参数
    let that=this;
    that.getHotSearch();
    let stype=options.stype;
    let eId=options.eId;
    let cirId=options.ID;
    if(stype==1){
      that.setData({forumArea:true,hotArea:false});
      wx.setNavigationBarTitle({ //动态设置头部标题
        title: '搜索'
      });
    }else if(eId==1){
      wx.setNavigationBarTitle({ //动态设置头部标题
        title: '擂盟详情'
      })
      that.data.circleParam.ID=cirId;
      that.data.circleParam.UserId=app.getStorage('customerId');
      that.data.circleParam.PageIndex = that.data.page.PageIndex + 1;
      that.data.circleParam.PageSize = that.data.page.PageSize;
      that.setData({searchArea:true,hotArea:true,forumArea:false});
      that.getCircleDetail();
    }else{
      wx.setNavigationBarTitle({ //动态设置头部标题
        title: '搜索结果'
      });
    }
  },
  onPullDownRefresh: function () {//下拉刷新
    let that = this;
    let page = { PageIndex: 0, PageSize: 10 };
    that.setData({ topics: [], page: page });
    that.enterDetail();
    wx.stopPullDownRefresh();
  },
  onReachBottom: function () {//上拉加载
    let that = this;
    that.getTopics();
  },
  getHotSearch:function(){//获取搜索热词
    let that=this;
    httptool.ajaxBasePost("api/HotWord/GetHotWord",{},function(res){//获取搜索热词
      if(res.suc){
        if(res.data!=""&& res.data!=null && res.data!=undefined){
            let data=res.data;
            if(data.length>0){
              that.setData({hotList:data});
            }
        }
      }else{
          app.showNotify(that, res.msg);
      }
    });
  },
  searchResult:function(e){//模糊查询输入一个字掉整个擂盟名称
    let that=this;
    httptool.ajaxBasePost("api/Circle/GetSearchCircle",{CircleName:that.data.CircleName},function(res){
      if(res.suc){
        let data = app.notNull(res.data, []);
        if(data.length>0){
            that.setData({ results_data:data,propName:false});
        }
      }else{
          app.showNotify(that, res.msg);
      }
    });
  },
  enterDetail:function(e){//进入搜索详情
    let that=this;
    that.setData({hotArea:true,propName:true,forumArea:false});
    let circleParam={};
    circleParam.ID=e.currentTarget.dataset.id;
    console.log("擂盟id是"+circleParam.ID);
    circleParam.UserId=app.getStorage('customerId') || '0';
    circleParam.PageIndex = that.data.page.PageIndex + 1;
    circleParam.PageSize = that.data.page.PageSize;
    that.setData({circleParam:circleParam});
    app.showLoading();
    that.getCircleDetail();
  },
  getCircleDetail:function(){//擂盟详情
    let that=this;
    // that.data.circleParam.UserId = -1;
    httptool.ajaxBasePost("api/Circle/GetCircleInfo",that.data.circleParam,function(res){
      if (res.suc) {  
        let data = res.data;
        that.setData({ CircleDetail: data });
        if (data.IsAdds == 1 || data.CircleType == 1) {//已加入          
          that.getTopics();
        }
        else {
          that.setData({ noData: true, noDataMsg: "没有加入该盟，无法查看帖子" });
        }
      } else {
        that.setData({ noData: that.data.topics.length == 0 });
        app.showNotify(that, res.msg);
      }
      wx.hideLoading();
      },function () { 
        wx.hideLoading();
    });
  },
  friendDetail:function(e){//点击头像进入某人的详情页，查看对方信息，帖子
    let that=this;
    let ToUserId = e.currentTarget.dataset.creatid;
    wx.navigateTo({
      url: '../followdetail/followdetail?ToUserId='+ToUserId,
    });
  },
  getTopics: function (){//获取帖子数据赋值
    let that=this;
    let column = that.data.circleParam;
    column.UserId=-1;
    column.PageIndex = that.data.page.PageIndex;
    httptool.ajaxBasePost("api/Topic/GetCircleWithTopic", that.data.circleParam, function (res) {
      if (res.suc) {
        if (res.data.length > 0) {
          let data = app.notNull(res.data, []);
          that.data.page.PageIndex += 1;
          for (let i in data) {
            data[i].IsDels = false;
          }
          data = that.data.topics.concat(data);
          that.setData({ resultForums: data, topics: data, page: that.data.page });
        }else{
          that.setData({ noData: that.data.topics.length == 0 });
        }
      }      
    });    
  },
 entDetail:function(e){//点击进入帖子详情
    let forumId = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/detailPost/detailPost?ID=' + forumId,
    });
  },
 thumbs_up: function (e) {//点赞事件
    let that = this;
    let isup=e.currentTarget.dataset.isup;
    let id = e.currentTarget.dataset.id;
    let index = e.currentTarget.dataset.index;
    let likeTimes = e.currentTarget.dataset.liketimes;
    let thumbs = {};
    thumbs.UserId = app.getStorage('customerId');
    thumbs.TopicId = id;
    httptool.ajaxPost("api/TopicZan/AddorDelLike", thumbs, function (res) {
      if (res.suc) {
        if(res.data==1){
          that.data.topics[index].LikeTimes = likeTimes + 1;
          that.setData({ topics: that.data.topics });
          wx.showToast({
            title: "点赞成功",
            duration: 1000,
            icon: "success"
          })
        }else if(res.data==0){
          that.data.topics[index].LikeTimes = likeTimes - 1;
          that.setData({ topics: that.data.topics });
          wx.showToast({
            title: "取消点赞",
            duration: 1000,
            icon: "success"
          })
        }
      } else {
          app.showNotify(that, res.msg);
      }
    });
  },
  enterForward:function(e){//点击进入转发页面
    let that=this;
    let fourumId=e.currentTarget.dataset.id;
    let Reference=e.currentTarget.dataset.reference;
    if(Reference!=0){
      wx.navigateTo({
        url: '/pages/forward/forward?fourumId='+Reference,
      });
    }else{
      wx.navigateTo({
        url: '/pages/forward/forward?fourumId='+fourumId,
      });
    }
  },
  goToforwardForum:function(e){//点击转发的帖子内容进入对应的帖子详情
    let ID=e.currentTarget.dataset.forumid;
    let UserId=app.getStorage('customerId');
    wx.navigateTo({
      url: '/pages/detailPost/detailPost?ID='+ID+"&UserId="+UserId,
    });
  },
  previewImg:function(e){//预览图片
    let src = e.currentTarget.dataset.src;//获取data-src
    let imgLists = e.currentTarget.dataset.list;//获取data-list
      //图片预览
     wx.previewImage({
       current: src, // 当前显示图片的http链接
       urls: imgLists // 需要预览的图片http链接列表
    })
  },
  editCircleName:function(e){
    let editmodel=e.currentTarget.dataset.editmodel;
    let id=e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/buildpart/buildpart?editmodel='+editmodel+"&ID="+id,
    });
  },
  joinCircle:function(e){//申请加入圈子
    let that=this;
    let id=e.currentTarget.dataset.id;
    let circleJoin={};
    circleJoin.ID=id;
    circleJoin.UserId=app.getStorage('customerId');
    httptool.ajaxPost("api/Circle/ApplyCircle",circleJoin, function (res) {
        if (res.suc) {
          wx.showToast({
            title: "等待审核",
            duration: 1000,
            icon: "success"
          });
          that.data.CircleDetail.IsAdds=0;
          that.setData({CircleDetail:that.data.CircleDetail});
        } else {
          app.showNotify(that, res.msg);
        }
    });
  },
})